//
//  B_TopicPictureView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/11.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_TopicPictureView.h"
#import "B_TopicPictureTVC.h"
#import "B_TopicPictureModel.h"

@implementation B_TopicPictureView

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor color_main_background_color];
        [self addSubview:self.tableView];
        [self setTableViewFrameWithTop:0 left:0 right:0 bottom:0];
        [self.tableView registerNib:[UINib nibWithNibName:@"B_TopicPictureTVC" bundle:nil] forCellReuseIdentifier:@"B_TopicPictureTVC"];
        [self setTableViewBlock];
    }
    return self;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void) setTableViewBlock {
    WeakSelf(weSelf, self);
    self.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
        return weSelf.dataArray.count;
    };
    self.tableView.pagingEnabled = YES;
    /// 组里的个数
    self.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
        return 1;
    };
    self.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        view.backgroundColor = [UIColor color_main_background_color];
        return view;
    };
    self.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        view.backgroundColor = [UIColor color_main_background_color];
        return view;
    };
    self.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
        return 0.1;
    };
    self.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
        return SCREEN_HEIGHT_X - 64 - 49;
    };
    self.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
        ///TODO:在这里取模型赋值
        // TODO:在这里加载数据
        if (weSelf.dataArray.count - indexPath.section == 3) {
            if (weSelf.topicPictureLoadMore) {
                weSelf.topicPictureLoadMore();
            }
        }
        if (weSelf.dataArray.count - indexPath.section == 1) {
            //TODO:弹出toast 没有数据了
        }
        
        B_TopicPictureModel * model = weSelf.dataArray[indexPath.section];
        B_TopicPictureTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_TopicPictureTVC" forIndexPath:indexPath];
        [cell.B_userImageView sd_setImageWithURL:[NSURL URLWithString:model.portrait]];
        cell.B_userName.text = model.nickName;
        [cell.B_imagView sd_setImageWithURL:[NSURL URLWithString:model.picture]];
        [cell.B_userTypeImageView setUsetType:model.userType];
        [cell.B_sexImageView mainpageSetSex:model.sex];
        cell.B_titleLabel.text = model.title;
        cell.B_contentLabel.text = model.content;
        cell.B_cityLabel.text = model.city;
        cell.B_ageLabel.text = [NSString stringWithFormat:@"%@岁", safeValue1(model.age)];
        cell.B_timeLabel.text = [NSObject getTimeTo:model.createTime];
        cell.B_collectionNumber.text = [NSObject getNumber:model.topicCollectCount];
        cell.B_commentNumber.text = [NSObject getNumber:model.topicCommentCount];
        return cell;
    };
}

@end
